
const userUtil = require('../../utils/user.js');
const productUtil = require('../../utils/productUtil.js');

Page({
  data: {
    userinfos: [],
    radio: 1
  },

  onLoad: function () {

    var userinfos = this.data.userinfos;
    var checkID = 0; //默认单选选中id
    for (var i in userinfos) {
      if (userinfos[i].status) {
        checkID = userinfos[i].id;
      }
    }

    this.setData({
      radio: checkID
    })

  },
  //单选
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
  //跳转至地址编辑页面
  modifyAddress: function (event) {

    var id = event.currentTarget.dataset.id;

    var userinfoStr = this.data.userinfos[id];

    var userinfo = JSON.stringify(userinfoStr);

    wx.navigateTo({
      url: '../modifyAddress/modifyAddress?status=modify&userinfo=' + userinfo,
    })
  },
  //新增地址
  addAddress: function () {
    wx.navigateTo({
      url: '../modifyAddress/modifyAddress?status=add',
    })
  },

  //获取返回页面的参数
  onShow: function () {
    this.getTabBar().init();
   
    this.getTabBar().setCartNumber(productUtil.getCacheCartProductIdList().size)

    this.setData({
      userinfos: userUtil.getAddressList()
    })

  },

  updateAddrList(){
    console.log(userUtil.getAddressList())
    this.setData({
      userinfos:userUtil.getAddressList()
    })
  },
  deleteAddr(event){
    let addrId = event.currentTarget.dataset.addrid
    userUtil.delAddressById(addrId)
    this.updateAddrList()

  }

})